package com.company.card_service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.company.card_service.mapper.EmployeeViewMapper;
import com.company.card_service.mapper.UserLoginLogMapper;
import com.company.card_service.model.Employee;
import com.company.card_service.mapper.EmployeeMapper;
import com.company.card_service.model.User;
import com.company.card_service.model.UserInfo;
import com.company.card_service.service.EmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.card_service.util.AliOssCloudUtil1;
import com.company.card_service.util.PageHelper;
import com.company.card_service.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 赵志浩
 * @since 2022-09-15 04:15:06
 */
@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private UserInfoServiceImpl userInfoService;

    @Autowired
    private AliOssCloudUtil1 aliOssCloudUtil1;

    @Autowired
    private EmployeeViewMapper employeeViewMapper;

    @Autowired
    private UserLoginLogMapper userLoginLogMapper;

    @Autowired
    private  UserRepository userRepository;
    @Override
    public UEmployeeVo getAllDataOfEmployee(Long id) {
        UEmployeeVo uEmployeeVo = employeeMapper.getAllDataOfEmployee(id);
        if(uEmployeeVo != null && uEmployeeVo.getList().size() > 0){
            uEmployeeVo.getList().stream().forEach(item->{
                LambdaQueryWrapper<UserInfo> lambdaQueryWrapper = new LambdaQueryWrapper();
                lambdaQueryWrapper.eq(UserInfo::getId,item.getMessage().getUserId());
                UserInfo one = userInfoService.getOne(lambdaQueryWrapper);
                item.setUserInfo(one);
            });
        }
        return uEmployeeVo;
    }
    //通过员工id查找员工信息
    @Override
    public Employee selectEmployeeById(int eid) {
        Employee employee = employeeMapper.selectById(eid);
        return employee;
    }


    //通过员工id冻结员工
    @Override
    public Result deleteEmployeeById(int id) {
//        QueryWrapper<Employee> employee = new QueryWrapper<>();
//        employee.eq("is_deleted",0);
//        One record is expected, but the query result is multiple records(注意报错一条数据查出来多条)
//        employeeMapper.selectOne(employee);
        Employee employee = employeeMapper.selectById(id);
        if (employee !=null){
            UpdateWrapper<Employee> wrapper = new UpdateWrapper<>();
            wrapper.set("is_deleted",1);
            int i = employeeMapper.update(null,wrapper);
            if (i == 0){
                return Result.fail(ResultCodeEnum.FAIL.getCode(),ResultCodeEnum.FAIL.getMessage());
            }else {
                return Result.success(ResultCodeEnum.SUCCESS.getCode(),ResultCodeEnum.SUCCESS.getMessage(),i);
            }
        }else {
            return Result.fail(ResultCodeEnum.FAIL.getCode(),ResultCodeEnum.FAIL.getMessage());
        }
    }

    @Override
    public void saveAll(MultipartFile files,Employee e) {
        //裁剪微信二维码图片
//        try{
//            InputStream inputStream = files.getInputStream();
//            BufferedImage bufferedImage = ImageIO.read(inputStream);
//            BufferedImage pic2 = bufferedImage.getSubimage(165,585 , 530, 525);
//            ByteArrayOutputStream os = new ByteArrayOutputStream();
//            ImageIO.write(pic2, files.getContentType(), os);
//            InputStream inputStreams = new ByteArrayInputStream(os.toByteArray());
//        }catch (IOException e1) {
//            e1.printStackTrace();
//        }
        //正常二维码图片
        String s = aliOssCloudUtil1.uploadFile(files);
        e.setWxCode(s);
        if(e.getId() != null){
            e.setUpdateTime(new Date());
            this.baseMapper.updateById(e);
        }
        else {
            e.setCreateTime(new Date());
            this.baseMapper.insert(e);
        }

    }

    @Override
    public PageHelper pageEmployeeQuery(EmployeeQueryVo employeeQueryVo, Integer companyid, Integer current, Integer size) {

        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getCompanyId,companyid);
        long count = employeeMapper.selectCount(lambdaQueryWrapper);
//        List<PCEmployeeVo> AllEmployeeQuery = employeeMapper.pageAllEmployeeQuery(employeeQueryVo.getId()
//                ,employeeQueryVo.getStartTime(),employeeQueryVo.getEndTime());
        current -=current;
        List<PCEmployeeVo> employeeQuery = employeeMapper.pageEmployeeQuery(companyid,employeeQueryVo.getId()
                ,employeeQueryVo.getStartTime(),employeeQueryVo.getEndTime(),current,size);
        int AllCount = Math.toIntExact(count);
        employeeQuery.stream().forEach(item->{
            User byId = userRepository.getById(item.getCreaterId());
            item.setCreateOr(byId.getName());
        });
        int PageCount = AllCount % size == 0 ? (AllCount/size) : (AllCount/size)+1;
        return new PageHelper(AllCount,PageCount,employeeQuery);
    }

    @Override
    public void saveAllFor(Employee e) {
        if(e.getId() != null){
            e.setUpdateTime(new Date());
            this.baseMapper.updateById(e);
        }
        else {
            e.setCreateTime(new Date());
            this.baseMapper.insert(e);
        }
    }

    @Override
    public PageHelper pageEmployeeTypeQuery(Integer current, Integer size, Integer typeId,Integer companyid) {

        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Employee::getCompanyId,companyid);
        long count = employeeMapper.selectCount(lambdaQueryWrapper);
        current -=current;
        List<PCEmployeeVo> employeeQuery = employeeMapper.pageEmployeeTypeQuery(current,size,typeId,companyid);
        int AllCount = Math.toIntExact(count);
        employeeQuery.stream().forEach(item->{
            User byId = userRepository.getById(item.getCreaterId());
            item.setCreateOr(byId.getName());
        });
        int PageCount = AllCount % size == 0 ? (AllCount/size) : (AllCount/size)+1;
        return new PageHelper(AllCount,PageCount,employeeQuery);
    }

    @Override
    public List<PCEmployeeVo> excellentEmployee(Integer companyId) {

        if(ObjectUtils.isNotEmpty(companyId)){

            List<PCEmployeeVo> employees = this.baseMapper.excellentEmployee(companyId);
            return employees;
        }
        return null;
    }
}
